//
//  Shui_shi_vc.m
//  ainkjj
//
//  Created by MAX on 2020/10/13.
//  Copyright © 2020 zhiyiLiu. All rights reserved.
//

#import "Shui_shi_vc.h"
#import "Cwgvc.h"
#import "WGBWaveLayerButton.h"
#import "Weishi_shezhi_vc.h"
#import "Shui_zidong_vc.h"

#import "DeviceConfig_res.h"
@interface Shui_shi_vc ()<lzyupdatecmd>
@property (nonatomic, strong)UITextField *weishiliang_tf;
@property (nonatomic, strong)UITextField *weishuiliang_tf;
@property (nonatomic, strong)UILabel *zidongweishi_status_lb;

@property (nonatomic, strong)UILabel *zidongweishui_status_lb;
@property (nonatomic, strong)DeviceConfig_res *feed_config ;
@property (nonatomic, strong)DeviceConfig_res *water_config;
@end

@implementation Shui_shi_vc
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    Cwgvc *pvc = (Cwgvc *)self.parentViewController;
    
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setObject:AuthSignin_model.sharedInstance.data.token forKey:@"Authorization"];
    [dict setObject:app_code forKey:@"app_code"];
    [dict setObject:pvc.entity.device_id forKey:@"device_id"];
    
    [dict setObject:@"set_water" forKey:@"type"];
    
    [PPHTTPRequest req:@"api/pets/App/getDeviceConfig" parameters:dict success:^(id response) {
        NSString *error_code = [NSString stringWithFormat:@"%@", response[@"error_code"]];
        if ([error_code isEqualToString:@"0"]) {
            DeviceConfig_res *water_config = [DeviceConfig_res mj_objectWithKeyValues:response];
            self.water_config = water_config;
            DeviceConfig_resList *l = water_config.data.list.firstObject;
            if (l.is_auto == 1) {
                self.zidongweishui_status_lb.text = @"已关闭";
            }else if(l.is_auto == 2){
                self.zidongweishui_status_lb.text = @"开启中";
            }else{
                self.zidongweishui_status_lb.text = @"";
            }
        }
        
        else
            //            if ([error_code isEqualToString:@""])
        {
            
            [MBProgressHUD showTipMessageInView:response[@"error_msg"]];
            
        }
    } failure:^(NSError *error) {
        [MBProgressHUD showTipMessageInView:[error localizedDescription]];
        
    }];
    
    dict = [NSMutableDictionary dictionary];
    [dict setObject:AuthSignin_model.sharedInstance.data.token forKey:@"Authorization"];
    
    [dict setObject:app_code forKey:@"app_code"];
    [dict setObject:pvc.entity.device_id forKey:@"device_id"];
    
    [dict setObject:@"set_feed" forKey:@"type"];
    
    [PPHTTPRequest req:@"api/pets/App/getDeviceConfig" parameters:dict success:^(id response) {
        NSString *error_code = [NSString stringWithFormat:@"%@", response[@"error_code"]];
        if ([error_code isEqualToString:@"0"]) {
            DeviceConfig_res *feed_config = [DeviceConfig_res mj_objectWithKeyValues:response];
            self.feed_config = feed_config;
            DeviceConfig_resList *l = feed_config.data.list.firstObject;
            if (l.is_auto == 1) {
                self.zidongweishi_status_lb.text = @"已关闭";
            }else if(l.is_auto == 2){
                self.zidongweishi_status_lb.text = @"开启中";
            }else{
                self.zidongweishi_status_lb.text = @"";
            }
            
        }
        
        else
            //            if ([error_code isEqualToString:@""])
        {
            
            [MBProgressHUD showTipMessageInView:response[@"error_msg"]];
            
        }
    } failure:^(NSError *error) {
        [MBProgressHUD showTipMessageInView:[error localizedDescription]];
        
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    UIView *v = UIView.new;
    [self.view addSubview:v];
    v.frame = CGRectMake(0, 0, ScreenWidth, 10);
    v.backgroundColor = UIColor.groupTableViewBackgroundColor;
    
    UILabel *weishiliang_lb = [[UILabel alloc ] initWithFrame:CGRectMake(10, CGRectGetMaxY(v.frame) + 20, 160, 20)];
    [self.view addSubview:weishiliang_lb];
    weishiliang_lb.text = @"喂食量（格）";
    
    UITextField *weishiliang_tf = [[UITextField alloc] initWithFrame:CGRectMake( CGRectGetMaxX(weishiliang_lb.frame), CGRectGetMinY(weishiliang_lb.frame), ScreenWidth - CGRectGetMaxX(weishiliang_lb.frame) - 20, 44)];
    [self.view addSubview:weishiliang_tf];
    self.weishiliang_tf = weishiliang_tf;
    weishiliang_tf.placeholder = @"请输入喂食量";
    weishiliang_tf.center = CGPointMake(weishiliang_tf.center.x, weishiliang_lb.center.y);
    
    [weishiliang_tf setTextAlignment:NSTextAlignmentRight];
    [weishiliang_tf setKeyboardType:UIKeyboardTypeNumberPad];
    
    UIView *sepv1 = UIView.new;
    [self.view addSubview:sepv1];
    sepv1.backgroundColor = UIColor.groupTableViewBackgroundColor;
    sepv1.frame = CGRectMake(0, CGRectGetMaxY(weishiliang_tf.frame) + 10, ScreenWidth, 1);
    
    UILabel *shoudongweishi_lb = [[UILabel alloc] initWithFrame:CGRectMake(10, CGRectGetMaxY(sepv1.frame) + 20, 100, 20)];
    [self.view addSubview:shoudongweishi_lb];
    shoudongweishi_lb.text = @"手动喂食";
    
    // 按钮效果
    WGBWaveLayerButton *weishi_btn = [[WGBWaveLayerButton alloc] initWithFrame:CGRectMake(ScreenWidth - 90, CGRectGetMinY(shoudongweishi_lb.frame), 80, 30)];
    [self.view addSubview:weishi_btn];
//    [weishi_btn setTitleColor:appMainColor forState:UIControlStateNormal];
    [weishi_btn setTitle:@"喂食" forState:UIControlStateNormal];
    //    weishi_btn.layer.borderColor = appMainColor.CGColor;
    //    weishi_btn.layer.borderWidth = 1;
    weishi_btn.layer.masksToBounds = YES;
    weishi_btn.layer.cornerRadius = 4;
    [weishi_btn.layer setQmui_maskedCorners:UIRectCornerAllCorners];
    [weishi_btn addTarget:self action:@selector(shi_action) forControlEvents:UIControlEventTouchUpInside];
    weishi_btn.backgroundColor = appMainColor;
    weishi_btn.waveLayerColor = [UIColor greenColor];//设置波纹的颜色
  
    UIView *sepzidong1 = UIView.new;
    [self.view addSubview:sepzidong1];
    sepzidong1.backgroundColor = UIColor.groupTableViewBackgroundColor;
    sepzidong1.frame = CGRectMake(0, CGRectGetMaxY(weishi_btn.frame) + 10, ScreenWidth, 1);
    
    UILabel *zidongweishi_lb = [[UILabel alloc] initWithFrame:CGRectMake(10, CGRectGetMaxY(weishi_btn.frame) + 30, 140, 20)];
    [self.view addSubview:zidongweishi_lb];
    zidongweishi_lb.text = @"自动喂食";
    
    UILabel *zidongweishi_status_lb = [[UILabel alloc] initWithFrame:CGRectMake(ScreenWidth - 80, CGRectGetMinY(zidongweishi_lb.frame), 80, 20)];
    [self.view addSubview:zidongweishi_status_lb];
    self.zidongweishi_status_lb = zidongweishi_status_lb;
    
    UIImageView *rightArrowiv = UIImageView.new;
    [self.view addSubview:rightArrowiv];
    rightArrowiv.frame = CGRectMake( ScreenWidth - 18, CGRectGetMinY(zidongweishi_status_lb.frame)+ 4, 8, 15);
    rightArrowiv.image = UIImageMake(@"mine_more_icon");
    UIButton *gaibtn = UIButton.new;
    [self.view addSubview:gaibtn];
    gaibtn.frame = CGRectMake(0, CGRectGetMinY(zidongweishi_lb.frame) - 10, ScreenWidth, 44);
    [gaibtn addTarget:self action:@selector(weishi_shezhi) forControlEvents:UIControlEventTouchUpInside];
    
    
    UIView *v2 = UIView.new;
    [self.view addSubview:v2];
    v2.frame = CGRectMake(0, CGRectGetMaxY(zidongweishi_lb.frame) + 20, ScreenWidth, 10);
    v2.backgroundColor = UIColor.groupTableViewBackgroundColor;
    
    UILabel *weishuiliang_lb = [[UILabel alloc ] initWithFrame:CGRectMake(10, CGRectGetMaxY(v2.frame) + 20, 180, 20)];
    [self.view addSubview:weishuiliang_lb];
    weishuiliang_lb.text = @"喂水时长（秒）";
    
    UITextField *weishuiliang_tf = [[UITextField alloc] initWithFrame:CGRectMake( CGRectGetMaxX(weishuiliang_lb.frame), CGRectGetMinY(weishuiliang_lb.frame), ScreenWidth - CGRectGetMaxX(weishuiliang_lb.frame) - 20, 44)];
    [self.view addSubview:weishuiliang_tf];
    self.weishuiliang_tf = weishuiliang_tf;
    weishuiliang_tf.placeholder = @"请输入喂水时长";
    weishuiliang_tf.center = CGPointMake(weishuiliang_tf.center.x, weishuiliang_lb.center.y);
    
    [weishuiliang_tf setTextAlignment:NSTextAlignmentRight];
    [weishuiliang_tf setKeyboardType:UIKeyboardTypeNumberPad];
    
    UIView *sepv2 = UIView.new;
    [self.view addSubview:sepv2];
    sepv2.backgroundColor = UIColor.groupTableViewBackgroundColor;
    sepv2.frame = CGRectMake(0, CGRectGetMaxY(weishuiliang_tf.frame) + 10, ScreenWidth, 1);
    
    UILabel *shoudongweishui_lb = [[UILabel alloc] initWithFrame:CGRectMake(10, CGRectGetMaxY(sepv2.frame) + 20, 100, 20)];
    [self.view addSubview:shoudongweishui_lb];
    shoudongweishui_lb.text = @"手动喂水";
    
    // 按钮效果
    
    {
        WGBWaveLayerButton *weishi_btn = [[WGBWaveLayerButton alloc] initWithFrame:CGRectMake(ScreenWidth - 90, CGRectGetMinY(shoudongweishui_lb.frame), 80, 30)];
        weishi_btn.backgroundColor = [[UIColor blueColor] colorWithAlphaComponent:0.5];;
        weishi_btn.waveLayerColor = [UIColor orangeColor];//设置波纹的颜色
        [self.view addSubview:weishi_btn];
//        [weishi_btn setTitleColor:appMainColor forState:UIControlStateNormal];
        [weishi_btn setTitle:@"喂水" forState:UIControlStateNormal];
        //    weishi_btn.layer.borderColor = appMainColor.CGColor;
        //    weishi_btn.layer.borderWidth = 1;
        weishi_btn.layer.masksToBounds = YES;
        weishi_btn.layer.cornerRadius = 4;
        [weishi_btn.layer setQmui_maskedCorners:UIRectCornerAllCorners];
        [weishi_btn addTarget:self action:@selector(shui_action) forControlEvents:UIControlEventTouchUpInside];
        weishi_btn.backgroundColor = appMainColor;
        weishi_btn.waveLayerColor = [UIColor greenColor];//设置波纹的颜色
        
        
        
        UIView *sepzidong1 = UIView.new;
        [self.view addSubview:sepzidong1];
        sepzidong1.backgroundColor = UIColor.groupTableViewBackgroundColor;
        sepzidong1.frame = CGRectMake(0, CGRectGetMaxY(weishi_btn.frame) + 10, ScreenWidth, 1);
        
        UILabel *zidongweishi_lb = [[UILabel alloc] initWithFrame:CGRectMake(10, CGRectGetMaxY(weishi_btn.frame) + 30, 100, 20)];
        [self.view addSubview:zidongweishi_lb];
        zidongweishi_lb.text = @"自动喂水";

        
        UILabel *zidongweishi_status_lb = [[UILabel alloc] initWithFrame:CGRectMake(ScreenWidth - 80, CGRectGetMinY(zidongweishi_lb.frame), 80, 20)];
        [self.view addSubview:zidongweishi_status_lb];
        self.zidongweishui_status_lb = zidongweishi_status_lb;
        
        UIImageView *rightArrowiv = UIImageView.new;
        [self.view addSubview:rightArrowiv];
        rightArrowiv.frame = CGRectMake( ScreenWidth - 18, CGRectGetMinY(zidongweishi_status_lb.frame)+4, 8, 15);
        rightArrowiv.image = UIImageMake(@"mine_more_icon");
        UIButton *gaibtn = UIButton.new;
        [self.view addSubview:gaibtn];
        gaibtn.frame = CGRectMake(0, CGRectGetMinY(zidongweishi_lb.frame) - 10, ScreenWidth , 44);
        [gaibtn addTarget:self action:@selector(weishui_shezhi) forControlEvents:UIControlEventTouchUpInside];
        
    }
    
    

    
}
- (void)shi_action{
    if (self.weishiliang_tf.text.length > 0) {
        Cwgvc *vc = (Cwgvc *)self.parentViewController;
        vc.shi_tf.text = self.weishiliang_tf.text;
        [vc shi:nil];
    }else{
        [QMUITips showInfo:@"请输入喂食量"];
    }
   
}
- (void)shui_action{
    if (self.weishuiliang_tf.text.length > 0) {

    Cwgvc *vc = (Cwgvc *)self.parentViewController;
    vc.shui_tf.text = self.weishuiliang_tf.text;
    [vc shui:nil];
    }else{
        [QMUITips showInfo:@"请输入喂水时长"];

    }
}
- (void)weishi_shezhi{
    Weishi_shezhi_vc *vc= [[Weishi_shezhi_vc alloc]init];
    vc.hidesBottomBarWhenPushed = YES;
     Cwgvc *pvc = (Cwgvc *)self.parentViewController;
    vc.device_id = pvc.entity.device_id;
    vc.delegate = self;
    vc.config = self.feed_config.data.list.firstObject;
    [self.parentViewController.navigationController pushViewController:vc animated:YES];
}
- (void)weishui_shezhi{
    Shui_zidong_vc *vc= [[Shui_zidong_vc alloc]init];
    vc.hidesBottomBarWhenPushed = YES;
    Cwgvc *pvc = (Cwgvc *)self.parentViewController;
    vc.device_id = pvc.entity.device_id;
    vc.delegate = self;
    vc.config = self.water_config.data.list.firstObject;
    [self.parentViewController.navigationController pushViewController:vc animated:YES];
}

- (void)queryWith_cmd:(NSString *)cmd data:(NSArray *)dataArr{
     Cwgvc *pvc = (Cwgvc *)self.parentViewController;
    [pvc queryWith_cmd:cmd data:dataArr];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
